/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.serverpackets;

import static l1j.server.server.model.skill.L1SkillId.STATUS_WISDOM_POTION;
import static l1j.server.server.model.skill.L1SkillId.ILLUSION_LICH;
import static l1j.server.server.model.skill.L1SkillId.ILLUSION_AVATAR;
import l1j.server.server.model.Instance.L1PcInstance;

public class S_SPMR extends ServerBasePacket {

	private static final String S_SPMR = "[S] S_S_SPMR";

	private byte[] _byte = null;

	public S_SPMR(final L1PcInstance pc) {
		this.buildPacket(pc);
	}

	private void buildPacket(final L1PcInstance pc) {
		this.writeC(S_OPCODE_SPMR);
		// ウィズダムポーションのSPはS_SkillBrave送信時に更新されるため差し引いておく
		 int sp =0;
		if (pc.hasSkillEffect(STATUS_WISDOM_POTION)) {
			sp = pc.getSp() - pc.getTrueSp() - 2; // 装備増加したSP
		} else {
			sp = pc.getSp() - pc.getTrueSp(); // 装備増加したSP
		}
		this.writeC(Math.min(127, sp));
		sp = pc.getSp();
		// TODO 魔法防禦封包修正by99團隊
		this.writeH(pc.getTrueMr() - pc.getBaseMr()); // 装備や魔法で増加したMR
		// TODO 魔法防禦封包修正by99團隊
		if(pc.hasSkillEffect(ILLUSION_LICH)){
			sp += 2;
		}
		if(pc.hasSkillEffect(ILLUSION_AVATAR)){
			sp += 10;
		}
		if (pc.checkLastSP()) {
			pc.sendPackets(new S_SystemMessage("魔法攻擊力變更為:" + sp));
		}
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this.getBytes();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return S_SPMR;
	}
}
